#! /usr/bin/env python
#coding=utf-8
'''
作者：gdlmo
邮箱：gytlgac@163.com
日期: 2017-01-16
版本：1.0
'''
import urllib.request
import re
from pyquery import PyQuery as pq
from lxml import etree
page = 1
url = 'http://www.qiushibaike.com/hot/page/' + str(page)
user_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64)'
headers = {'User-Agent' : user_agent}
def getPqs(content, selector):
    doc = pq(content)
    lists = doc(selector)
    return lists

try:
    request = urllib.request.Request(url, headers=headers)
    response = urllib.request.urlopen(request)
    content = response.read().decode('utf-8')


    # body 是元素，如div ，div.article 是div元素且class是article, div.conetnt是div元素且class为content
    lists = getPqs(content, 'body div.article')
    #print(len(items))      #查看items的长度
    #print(type(items))     #查看items的类型
    for item in lists.items():  #注意：此处犯过一个错误，要使用lists.items
        its = getPqs(item.html(), 'div')
        for it in its.items():
            print(it.text())
except urllib.request.URLError as e:
    if hasattr(e, 'code'):
        print(e.code)
    if hasattr(e, 'reason'):
        print(e.reason)